/**
  ******************************************************************************
  * @file    common.c
  * @author  zqj
  * @version v1.0
  * @date    2016-02-25
  * @brief   用了存放公用函数
  ******************************************************************************
	*/
//#include "M_Global.h"
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const uint8_t auchCRCHi[] =
{
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40,
};

const uint8_t auchCRCLo[] =
{
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
0x40
};

uint8_t Common_isAll(uint8_t *updata, uint16_t len)
{
    for (uint16_t i = 1; i < len; i++)
    {
        if(updata[i-1] != updata[i])
        {
            return (updata[0] + 1);
        }
    }
    return updata[0];
}
const char conNumber[] = {'0','1','2','3','4','5','6','7','8','9'\
                         ,'A','B','C','D','E','F'};


uint16_t Common_Crc16_Calc(uint8_t *updata, uint16_t len)
{
		unsigned short i;
		unsigned char uchCRCHi = 0xff;
		unsigned char uchCRCLo = 0xff;
		unsigned short uindex;
		for (i = 0; i < len; i++)
		{
				uindex = (unsigned short)(uchCRCHi ^ updata[i]);
				uchCRCHi = (unsigned char)(uchCRCLo ^ auchCRCHi[uindex]);
				uchCRCLo = auchCRCLo[uindex];
		}
		return (unsigned short)(uchCRCHi << 8 | uchCRCLo);
}

uint8_t Common_Hex2bcd8(uint8_t hex)
{
	return ((hex%10) + ((hex/10)<<4));
}
uint8_t Common_Bcd2hex8(uint8_t bcd)
{
	return ((bcd&0xf) + ((bcd&0xf0)>>4)*10);
}
uint8_t Common_Bcd_Decimal(uint8_t bcd)
{
	 return bcd-(bcd>>4)*6;
}
uint8_t Common_Decimal_Bcd(uint8_t decimal)
{
	 return decimal+(decimal/10)*6;
}

uint32_t Common_Bcd2hex32(uint32_t bcd)
{
	uint32_t hex32;

	hex32 =   bcd&0xf;
	hex32 +=  ((bcd&0x000000f0)>>4)*10;
	hex32 +=  ((bcd&0x00000f00)>>8)*100;
	hex32 +=  ((bcd&0x0000f000)>>12)*1000;
	hex32 +=  ((bcd&0x000f0000)>>16)*10000;
	hex32 +=  ((bcd&0x00f00000)>>20)*100000;
	hex32 +=  ((bcd&0x0f000000)>>24)*1000000;
	hex32 +=  ((bcd&0xf0000000)>>28)*10000000;
	return hex32;
}

uint32_t Common_Hex2bcd32(uint32_t hex)
{
	uint32_t bcd;
	if(hex > 100000000)
	{
		return 0xffffffff;
	}
	bcd = (hex/10000000)<<28;
	bcd += ((hex%10000000)/1000000)<<24;
	bcd += ((hex%1000000)/100000)<<20;
	bcd += ((hex%100000)/10000)<<16;
	bcd += ((hex%10000)/1000)<<12;
	bcd += ((hex%1000)/100)<<8;
	bcd += ((hex%100)/10)<<4;
	bcd += (hex%10);
	return bcd;
}


uint32_t Common_CalcBCD32(uint32_t largeBCD, uint32_t smallBCD)
{
 	uint32_t LargeHex, smallHex;
	LargeHex = Common_Bcd2hex32(largeBCD);
	smallHex = Common_Bcd2hex32(smallBCD);
	if(LargeHex < smallHex)
	{
		return 0xffffffff;
	}
	return Common_Hex2bcd32(LargeHex - smallHex);
}

uint16_t Common_getLittleValue(uint16_t bigValue)
{
	uint16_t littleValue = 0;
	littleValue = ((bigValue&0x00ff)<<8) | ((bigValue&0xff00)>>8);
	return littleValue;
}

uint16_t Common_getBigValue(uint16_t littleValue)
{
	uint16_t bigValue = 0;
	bigValue = (littleValue&0x00ff)<<8|(littleValue&0xff00)>>8;
	return bigValue;
}


int16_t Common_getPNBigValue(int16_t littleValue)
{
	int16_t bigValue = 0;
	bigValue = (littleValue&0x00ff)<<8|(littleValue&0xff00)>>8;
	return bigValue;
}

int16_t Common_getPNLittleValue(int16_t BigValue)
{
	int16_t LittleValue = 0;
	LittleValue = (BigValue&0x00ff)<<8|(BigValue&0xff00)>>8;
	return LittleValue;
}

uint32_t Common_get2BigValue(uint32_t littleValue)
{
	uint32_t bigValue = 0;
	bigValue = ((littleValue&0xff)<<24)|((littleValue&0xff00)<<8)|((littleValue&0xff0000)>>8)|((littleValue&0xff000000)>>24);
	return bigValue;
}

uint32_t Common_get2LittleValue(uint32_t BigValue)
{
	uint32_t LittleValue = 0;
	LittleValue = (((BigValue&0xff000000)>>24)|((BigValue&0xff0000)>>8)|((BigValue&0xff00)<<8)|((BigValue&0xff)<<24));
	return LittleValue;
}

uint16_t  Common_CalcU16Sum(uint16_t *pbuf, uint16_t len)
{
     uint32_t sum = 0;

     while (len > 1) {
         sum += *pbuf++;
         len -= sizeof(uint16_t);
     }
     if (len) {
         sum += *(uint8_t *)pbuf;
     }
     while (sum >> 16) {
         sum = (sum >> 16) + (sum & 0xffff);
     }

     return (uint16_t)(~sum);
}
uint32_t Common_CalclongSum(uint8_t * pbuf, uint16_t lenth)
{
	uint32_t sum = 0;
	while(lenth--)
	{
		sum += *pbuf;
		pbuf++;
	}
	return sum;
}


uint16_t Common_CalculateSum(uint8_t * pbuf, uint16_t lenth)
{
	uint16_t sum = 0;
	while(lenth--)
	{
		sum += *pbuf;
		pbuf++;
	}
	return sum;
}

uint8_t Common_CalcByteSum(uint8_t * pbuf, uint8_t lenth)
{
	uint8_t sum = 0;
	while(lenth--)
	{
		sum += *pbuf;
		pbuf++;
	}
	return sum;
}


uint8_t Common_CalculateNOR(uint8_t *p)
{
	uint8_t redata;
	uint16_t i;
	uint16_t len;

	len = p[1]*256+p[2];
	redata = p[0]^p[1];
	for(i=2;i<len+4;i++)
	{
		 redata = redata^p[i];
	}
	return 	redata;
}

/***********************************************************************
功能： Common_Change2Byte
参数:  交换两个字节的顺序
返回值：交换后的结果
*************************************************************************/
uint16_t Common_Change2Byte(uint16_t *data)
{
	 stoc_u tmp ;
	 tmp.c[0] = ((stoc_u*)data)->c[1];
	 tmp.c[1] = ((stoc_u*)data)->c[0];
	 *data = tmp.s;
	 return tmp.s;
}




/***********************************************************************
功能： Common_Change4Byte
参数:  交换4个字节的顺序
返回值：交换后的结果
*************************************************************************/
uint32_t Common_Change4Byte(uint32_t *data)
{
	 u32toc_u *ptr = (u32toc_u *)data;
	 u32toc_u tmp;

	 tmp.c[0] = ptr->c[3];
	 tmp.c[1] = ptr->c[2];
	 tmp.c[2] = ptr->c[1];
	 tmp.c[3] = ptr->c[0];

	 ptr->i = tmp.i;

	 return tmp.i;
}
/***********************************************************************
功能： Common_crc16lw
参数:  可分段校验，crc上次校验值，第一次crc=0
返回值：校验结果
*************************************************************************/
uint16_t Common_crc16lw(uint8_t *ptr,uint16_t crc, uint16_t len)
{
	uint8_t i;

	while(len--!=0)
	{
		for(i=0x01; i!=0; i <<= 1)
		{
			if((crc&0x0001)!=0)
			{
				crc >>= 1;
				crc ^= 0x8408;
			}
			else
			crc >>= 1;
			if((*ptr&i)!=0)
				crc ^= 0x8408;
		}
		ptr++;
	}
	return(crc);
}
/***********************************************************************
*函数名称：Common_ModbusCRC
*功能描述：计算crc值
*    参数：
*  返回值：
*************************************************************************/
uint16_t Common_ModbusCRC(uint8_t *data, uint16_t len)
{
	int16_t i, j;
	uint16_t crc=0xFFFF, flag=0;

	for (i = 0; i < len; i++)
	{
		crc ^= data[i];
		for (j = 0; j < 8; j++)
		{
			flag = crc & 0x0001;
			crc >>= 1;
			if (flag)
			{
				crc ^= 0xA001;
			}
		}
	}
	return crc;
}


/***********************************************************************
*函数名称：Common_BcdToAsc
*功能描述：将BCD码串转换成ASC码
*    参数：*str,*bcd,bynum
*  返回值：字符串长度,
*************************************************************************/
uint8_t Common_BcdToAsc(uint8_t *str,uint8_t *bcd,uint8_t bynum)    //
{
	 uint8_t i;
	 uint8_t *ptr = str;

	 for(i = 0; i < bynum; i++)
	 {
		 *ptr++ = conNumber[((bcd[i]>>4) & 0x0f)] ;
		 *ptr++ = conNumber[(bcd[i] & 0x0f)] ;
	 }
	 *ptr++ = 0;
	 return bynum*2;
}

/***********************************************************************
*函数名称：Common_NormlBcdToAsc
*功能描述：将BCD码串转换成ASC码,顺序不变
*    参数：*str,*bcd,bynum
*  返回值：字符串长度,
*************************************************************************/
uint8_t Common_NormlBcdToAsc(uint8_t *str,uint8_t *bcd,uint8_t bynum)
{
	 uint8_t i;
	 uint8_t *ptr = str;

	 for(i = 0; i < bynum ;i++ )
	 {
		 *ptr++ = conNumber[((bcd[i]>>4) & 0x0f)] ;
		 *ptr++ = conNumber[(bcd[i] & 0x0f)] ;
	 }
	 *ptr++ = 0;
	 return bynum*2;
}




/***********************************************************************
*函数名称：Common_IntToFloatStr
*功能描述：将整数变成浮点数字符串
*    参数：*str,data,dotpos小数位数
*  返回值：小数位数，限制为最大2位
*************************************************************************/
uint8_t Common_IntToFloatStr(uint8_t *str,int32_t data,uint8_t dotpos)
{
	 uint8_t i;
	 uint8_t fg;
	 uint32_t tmp,inttmp,ftmp;
	 char dotstr[10] = {0};
   char tmpstr[20];
   fg = 0;
   if( data & 0x80000000 ) {
		 fg = 1;
		 data *= -1;
	 }

	 tmp = 1;
	 for( i = 0 ; i < dotpos ; i++ ) {
		  tmp = tmp * 10 ;
	 }

	 inttmp = data / tmp;

	 ftmp = data % tmp;

	 sprintf((char *)tmpstr,"%d",inttmp);

	 dotstr[0] = '.';

	 for( i = 1; i < dotpos+1; i++)
	 {
			tmp = tmp/10;
		  if(i == dotpos)
			{
				dotstr[i] = conNumber[ftmp%10];
			}
			else
			{
				dotstr[i] = conNumber[ftmp/tmp];
				ftmp = ftmp%tmp;
			}
	 }
	 dotstr[i] = 0;

	 if(fg == 1 ) {
		str[0] = '-';
		str[1] = 0;
		strcat((char *)str,tmpstr);
	 }else {
		 strcpy((char *)str,tmpstr);
	 }
	 strcat((char *)str,dotstr);

	return strlen((char *)str);
}


/***********************************************************************
*函数名称：Common_IntToFloatStr
*功能描述：将IP地址字符串格式化成整数
*    参数：
*  返回值：
*************************************************************************/

uint8_t Common_FormatIpAddr(char *str,uint8_t ipv4[])
{
	 uint8_t len,i,cnt;
	 uint8_t  tmp8; //data[4][3] = {{0}};


   //j = 0;
   cnt = 0;
   len = strlen(str);

   memset(ipv4,0,4);

   for( i = 0; i < len; i++ )
   {
		  if( (str[i] <'0' || str[i] > '9') && str[i] != '.' ) {
				break;
			}

		  if((str[i] == '.')&& (cnt < 4)) {
				 cnt++;
				 continue;
			}

			if( (str[i+1] == '.') || (str[i+1] == 0xff) || (str[i+1] == 0x00) || (str[i+1] <'0' || str[i+1] > '9') ) {
				tmp8 = str[i]-'0';
		    ipv4[cnt] += tmp8;
				continue;
			}

			tmp8 = str[i]-'0';
		  ipv4[cnt] += tmp8;
			ipv4[cnt] *= 10;

	 }
	 if(cnt<3){
		 return COMMON_RTN_ERR;
	 }
	 else{
	   return COMMON_RTN_SUCC;
	 }

}




/***********************************************************************
*Common_FormatMacAddr
*功能描述：将mac地址字符串格式化成整数
*    参数：
*  返回值：
*************************************************************************/

uint8_t Common_FormatMacAddr(char *str,uint8_t mac[])
{
	 uint8_t len,i,j;
   uint16_t temp,temp2;
   char c;

   j = 0;
   len = strlen(str);
	 if( len < 17 ) {
		  return COMMON_RTN_ERR;
	 }


  memset(mac,0,6);

	for( i = 0; i < 6; i++ )
	{
		temp = 0;
		temp2 = 0;
		c = str[j];

		if( j >= 18 ) {
			break;
		}

		while( c == ':' ) {
			j++;
			c = str[j];
		}

		if( c >= 'a' && c <= 'f' ) // 两个字符中的第一个 比如 "0f" ,则表示是字符 '0'
		temp = ( c - 'a' ) + 10;
		else if ( c >= 'A' && c <= 'F' )
		temp = ( c - 'A' ) + 10;
		else
		temp = ( c - '0' ) ;
		j++;


		c = str[j];
		if( c >= 'a' && c <= 'f' ) // 两个字符中的第二个,如 "f8" ,那么表示字符 '8'
		temp2 = ( c - 'a' ) + 10;
		else if ( c >= 'A' && c <= 'F' )
		temp = ( c - 'A' ) + 10;
		else
		temp2 = ( c - '0' ) ;


		temp = temp * 16;
		temp += temp2;
		j++;
		*(mac+i) = temp;
	}


	return COMMON_RTN_SUCC;


}

/***********************************************************************
功能： Common_Change2Byte
参数:  交换两个字节的顺序
返回值：交换后的结果
*************************************************************************/
uint16_t Common_Change2ByteBigType(uint16_t data)
{
	 stoc_u tmp,*ptr;
	 ptr = (stoc_u *)&data;
	 tmp.c[0] = ptr->c[1];
	 tmp.c[1] = ptr->c[0];

	 return tmp.s;
}



/***********************************************************************
*Common_atol
*功能描述：将字符串变成整数
*    参数：
*  返回值：
*************************************************************************/
uint32_t Common_atol(char *str)
{
	return atol(str);
}


/***********************************************************************
*trim
*功能描述：去掉字符串末尾的回车、换行、空格
*    参数：
*  返回值：
*************************************************************************/
char *Common_trim(char *str)
{
	  uint16_t len = strlen(str);
    char *p = str;
    while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') {
        p ++;
			if( p > (str+len)) {
				return str;
			}
		}
    str = p;
    p = str + strlen(str) - 1;
    while (*p == 0x20 || *p == '\t' || *p == '\r' || *p == '\n' || *p == 0xff){
        -- p;
			if( p == str ) {
				 break;
			}
		}
    *(p + 1) = 0;
    return str;
}


/***********************************************************************
*Common_AscToBcd
*功能描述：将ASC码转换成BCD码
*    参数：
*  返回值：
*************************************************************************/
uint8_t Common_AscToBcd(uint8_t *bcd,uint8_t *str)
{
	 //uint8_t tmp;
	 uint8_t i,bynum;
	 uint8_t *ptr = bcd;
	 uint8_t buf[64] = {0};
	 bynum = strlen((char *)str)%60 ;
   memcpy(buf,str,bynum);
	 bynum += 1;

	 for(i = 0; i < bynum/2;i++)
	 {
		 if(buf[i*2] < 0x40 ) {
				*ptr = ((buf[i*2] - 0x30 ) & 0x0f )<< 4;
		 }else {
			 *ptr  = ((buf[i*2] - 0x41 + 10) & 0x0f ) << 4;
		 }
		 if(buf[i*2+1] < 0x40 ) {
				*ptr |= ((buf[i*2+1] -0x30) & 0x0f) ;
		 }else {
			 *ptr |= ((buf[i*2+1] -0x41 + 10 ) & 0x0f) ;
		 }
		  ptr++;
	 }
	 *ptr++ = 0;
	 return bynum/2;
}
/* 16位按小端模式转换为2个8位数*/
void Common_LittleShortTo(uint8_t *pbuf,uint16_t utemp)
{
	stoc_u temp;
	temp.s = utemp;
	pbuf[0] = temp.c[0];
	pbuf[1] = temp.c[1];
}
void Common_bigShortTo(uint8_t *pbuf,uint16_t utemp)
{
	stoc_u temp;
	temp.s = utemp;
	pbuf[0] = temp.c[1];
	pbuf[1] = temp.c[0];
}
/* 32位按小端模式转换为4个8位数*/
void Common_LittleIntTo(uint8_t *pbuf,uint32_t utemp)
{
	u32toc_u temp;
	temp.i = utemp;
	pbuf[0] = temp.c[0];
	pbuf[1] = temp.c[1];
	pbuf[2] = temp.c[2];
	pbuf[3] = temp.c[3];

}
void Common_bigIntTo(uint8_t *pbuf,uint32_t utemp)
{
	u32toc_u temp;
	temp.i = utemp;
	pbuf[0] = temp.c[3];
	pbuf[1] = temp.c[2];
	pbuf[2] = temp.c[1];
	pbuf[3] = temp.c[0];

}
/*将4个8位数据转成小端32位*/
uint32_t Common_LittleToInt(uint8_t *pbuf)
{
	u32toc_u temp;

	temp.c[0] = pbuf[0];
	temp.c[1] = pbuf[1];
	temp.c[2] = pbuf[2];
	temp.c[3] = pbuf[3];
	return temp.i;
}
/*将2个8位数据转成小端16位*/
uint16_t Common_LittleToShort(uint8_t *pbuf)
{
	stoc_u temp;

	temp.c[0] = pbuf[0];
	temp.c[1] = pbuf[1];
	return temp.s;
}

/*将2个8位数据转成大端16位*/
uint16_t Common_bigToShort(uint8_t *pbuf)
{
	stoc_u temp;

	temp.c[0] = pbuf[1];
	temp.c[1] = pbuf[0];
	return temp.s;
}
uint16_t HexToAsc(uint8_t Hex)
{
	stoc_u tmp16;
 	uint8_t tmp;
	tmp16.s = 0;

	tmp = (Hex >> 4)&0x0f;
	if( tmp > 9 ) {
		tmp16.c[1] = 'A' + (tmp-10);
	}else {
		tmp16.c[1] = '0' + tmp;
	}

	tmp = Hex & 0x0f;
	if( tmp > 9 ) {
		tmp16.c[0] = 'A' + (tmp-10);
	}else {
		tmp16.c[0] = '0' + tmp;
	}
	return tmp16.s;
}

/*大端模式转换*/
uint8_t TowAscToHex(uint8_t *buf)
{
	uint8_t tmp = 0;
	if(buf[0] >= 'A' && buf[0] <= 'F') {
		 tmp = buf[0] - 'A'+10;
	}else if( buf[0] >= '0' && buf[0] <= '9' ){
		tmp = buf[0] -'0';
	}
	tmp <<= 4;

	if(buf[1] >= 'A' && buf[1] <= 'F') {
		tmp += buf[1] - 'A'+10;
	}else if( buf[1] >= '0' && buf[1] <= '9' ){
		tmp += buf[1] -'0';
	}
	return tmp;
}
uint8_t OneAscToHex(uint8_t *buf)
{
	uint8_t tmp = 0;
	if(buf[0] >= 'A' && buf[0] <= 'F') {
		 tmp = buf[0] - 'A'+10;
	}else if( buf[0] >= '0' && buf[0] <= '9' ){
		tmp = buf[0] -'0';
	}
	return tmp;
}

/*数字字符串转换成整形数，不支持负数
*返回值：32位无符号整型数
*/
uint32_t Common_Ascii2Uint32(uint8_t *str,uint8_t len)
{
	uint8_t i ;
	uint32_t u32data = 0;
	for( i = 0 ; i < len ; i++ ) {
		if( str[i] >= '0' && str[i] <= '9') {
			u32data = str[i] - '0' + u32data * 10;
		}else {
			break;
		}
	}
	return u32data;
}


/*卡号中间4位变成***  */
uint8_t Common_TranCarNo(uint8_t *CardNo,uint8_t len)
{
	//uint8_t len = strlen((char *)CardNo);
	uint8_t *ptr = CardNo;
	if( len < 7 ) {
		return len;
	}
	ptr[3] = '*';
	ptr[4] = '*';
	ptr[5] = '*';
	ptr[6] = '*';
	return len;
}
uint16_t Common_tranHex(uint8_t *str)
{
	  uint16_t i,rtn=0;
	 uint16_t len;

	 len = strlen((const  char *)str);
   for(i = 0; i < len; i++)
   {
		  if( str[i] >= '0' && str[i] <= '9' ) //
		      rtn=(rtn*10)+(str[i]-48);
	    else break;
   }
   return 	rtn;
}
